package com.neomanager.stats.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.neomanager.login.model.AdminInfo;
import com.neomanager.stats.dao.StatsDao;

@Controller
@SessionAttributes("adminInfo")
public class StatsController {

	private final Logger logger = Logger.getLogger(this.getClass());
	@Resource(name="StatsDao")
	StatsDao stats;

	/**
	 * 접속통계
	 */
	@RequestMapping(value = "/stats/access.do", method = RequestMethod.GET)
	public String viewStatsAccessl(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("접속통계.");

		HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("adminInfo", aInfo);

		return "stats/stats_access";
	}

	/**
	 * 페이지뷰
	 */
	@RequestMapping(value = "/stats/pageview.do", method = RequestMethod.GET)
	public String viewStatsPageviewl(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("페이지뷰.");

		HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("adminInfo", aInfo);

		return "stats/stats_pageview";
	}

	/**
	 * 게시판별
	 */
	@RequestMapping(value = "/stats/stats_bbs.do", method = RequestMethod.GET)
	public String viewStatsBbsl(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("게시판별.");

		HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("adminInfo", aInfo);

		return "stats/stats_bbs";
	}

	/**
	 * 총방문자 수
	 */
	@RequestMapping(value = "/common/stats_total.do", method = RequestMethod.GET)
	public String listStatsTotal(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("총방문자 수.");

		ArrayList<Map<String, Object>> listMap = stats.selectTotalStats(commandMap);

		model.addAttribute("listMap", listMap);
		
		return "stats/stats_total";
	}

	/**
	 * 일별 접속통계/페이지뷰 목록
	 */
	@RequestMapping(value = "/common/stats_day.do", method = RequestMethod.GET)
	public String listStatsDay(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("일별 접속통계/페이지뷰 목록.");

		SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat ("yyyy-MM", Locale.KOREA );
		Date currentTime = new Date();
		String mTime = mSimpleDateFormat.format ( currentTime );

		String input_date = commandMap.get("input_date").toString();
		if (input_date == null || input_date.equals("")) input_date = mTime;

		commandMap.put("input_date", input_date);
		ArrayList<Map<String, Object>> listMap = stats.selectDayStats(commandMap);

		model.addAttribute("commandMap", commandMap);
		model.addAttribute("listMap", listMap);
		
		return "stats/stats_day";
	}

	/**
	 * 월별 접속통계/페이지뷰 목록
	 */
	@RequestMapping(value = "/common/stats_month.do", method = RequestMethod.GET)
	public String listStatsMonth(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("월별 접속통계/페이지뷰 목록.");

		SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat ("yyyy", Locale.KOREA );
		Date currentTime = new Date();
		String mTime = mSimpleDateFormat.format ( currentTime );

		String input_date = commandMap.get("input_date").toString();
		if (input_date == null || input_date.equals("")) input_date = mTime;

		commandMap.put("input_date", input_date);
		ArrayList<Map<String, Object>> listMap = stats.selectMonthStats(commandMap);

		model.addAttribute("commandMap", commandMap);
		model.addAttribute("listMap", listMap);
		
		return "stats/stats_month";
	}

	/**
	 * 년도별 접속통계/페이지뷰 목록
	 */
	@RequestMapping(value = "/common/stats_year.do", method = RequestMethod.GET)
	public String listStatsYear(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("년도별 접속통계/페이지뷰 목록.");

		ArrayList<Map<String, Object>> listMap = stats.selectYearStats(commandMap);

		model.addAttribute("commandMap", commandMap);
		model.addAttribute("listMap", listMap);

		return "stats/stats_year";
	}

	/**
	 * 게시판별 등록현황 목록
	 */
	@RequestMapping(value = "/common/stats_bbsreg.do", method = RequestMethod.GET)
	public String listStatsBbsreg(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("게시판별 등록현황 목록.");

		ArrayList<Map<String, Object>> listMap = stats.selectBBSTotalStats(commandMap);

		model.addAttribute("commandMap", commandMap);
		model.addAttribute("listMap", listMap);

		return "stats/stats_bbsreg";
	}
	
	/**
	 * 게시판별 통계 다운로드
	 * @throws Exception 
	 */
	@RequestMapping(value = "/stats/excel_download.do", method = RequestMethod.POST)
	public String statsExcelDownload(
			@RequestParam Map<String, Object> commandMap, 
			Model model, 
			ArrayList<Map<String, Object>> listMap,
			ArrayList<String> firstRowList) throws Exception
	{
		logger.info("엑셀 다운로드.");

		SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat ("yyyy-MM", Locale.KOREA );
		Date currentTime = new Date();
		String mTime = mSimpleDateFormat.format ( currentTime );

		String input_date = commandMap.get("input_date").toString();
		if (input_date == null || input_date.equals("")) input_date = mTime;

		commandMap.put("input_date", input_date);
		
		// 출력 데이터 선택
		if(commandMap.get("title").toString().contains("day"))
		{
			listMap = stats.selectDayStats(commandMap);
			commandMap.put("type", "day");
		}
		else if(commandMap.get("title").toString().contains("month"))
		{
			listMap = stats.selectMonthStats(commandMap);
			commandMap.put("type", "month");
		}
		else if(commandMap.get("title").toString().contains("year"))
		{
			listMap = stats.selectYearStats(commandMap);
			commandMap.put("type", "year");
		}
		else if(commandMap.get("title").toString().contains("bbs"))
		{
			listMap = stats.selectBBSTotalStats(commandMap);
			commandMap.put("type", "bbs");
		}

		// 컬럼명 정의
		if(commandMap.get("tableName").toString().contains("access"))
		{
			firstRowList.add("날짜");
			firstRowList.add("방문자수");
			firstRowList.add("비율 (%)");
		}
		else if(commandMap.get("tableName").toString().contains("pageview"))
		{
			firstRowList.add("날짜");
			firstRowList.add("페이지뷰");
			firstRowList.add("비율 (%)");
		}
		else if(commandMap.get("tableName").toString().contains("bbs"))
		{
			firstRowList.add("게시판명");
			firstRowList.add("등록수");
			firstRowList.add("조회수");
		}
		
		model.addAttribute("firstRowList", firstRowList);
		model.addAttribute("commandMap", commandMap);
		model.addAttribute("listMap", listMap);
		
		return "excelDownload";
	}
}
